package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Product;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ProDao {
	private DBHelp<Product> db=new DBHelp<Product>();
	
	public void save(Product pro){
		String sql="insert into t_pro(id,proName,price,onsale) values(?,?,?,?)";
		db.executeSQL(sql,pro.getId(),pro.getProName(),pro.getPrice(),pro.isOnsale());
	}
	 
	public List<Product> findAll() {
		String sql = "select id,proName,price,onsale from t_pro";
		return db.executeQueryForList(sql, new productRowMapper());
	}
	public Product findById(String id) {
		String sql = "select id,proName,price,onsale from t_pro where id = ?";
		return db.executeQueryForObject(sql, new productRowMapper(), id);
	}
	public void del(String id){
		String sql="delete from t_pro where id=?";
		 db.executeSQL(sql, id);
	}
	public void update(Product pro){
		String sql="update t_pro set id=?,proName=?,price=?,onsale=? ";
		db.executeSQL(sql, pro.getId(),pro.getProName(),pro.getPrice(),pro.isOnsale());
		
	}
	private class productRowMapper implements RowMapper<Product>{

		public Product mapperRow(ResultSet rs) throws SQLException {
			Product pro=new Product();
			pro.setId(rs.getString("id"));
			pro.setPrice(rs.getString("price"));
			pro.setProName(rs.getString("proName"));
			pro.setOnsale(rs.getBoolean("onsale"));
			
			return pro;
		}
	}
}
